package com.coocaa.ops.admin.v1.timedtask.validator;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.timedtask.constant.TimedTaskTypeConstants;
import com.coocaa.ops.common.core.base.enums.ResourceStatusEnums;
import com.coocaa.ops.common.core.biz.timedtask.entity.TimedTaskInfoEntity;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author bijiahao
 * @date : 2019/8/7.
 * @description 定时任务-校验器
 *
 */
@Component
public final class TimedTaskValidator {
    private static CommonLanguageProperties commonLanguageProperties;

    /**
     * 间隔时间 2分钟
     * 如果距离任务执行只有2分钟左右,不允许取消
     * 避免取消定时操作  与定时任务执行【定时任务可能提前或者延迟执行】产生冲突
     */
    private static final int INTERVAL_TIME = 120 * 1000;

    /**
     * 定时任务不存在
     *
     * @param currentTimedTask
     * @return
     */
    public static boolean isNoExistTask(TimedTaskInfoEntity currentTimedTask) {
        return null == currentTimedTask;
    }

    /**
     * 定时任务不能取消-距离触发只有2分钟
     *
     * @param intervalTime
     * @return
     */
    public static boolean isCanNotCancelTask(long intervalTime) {
        return intervalTime < INTERVAL_TIME && intervalTime > 0;
    }

    /**
     * 定时任务要重新执行-定时已到没有执行情况
     *
     * @param intervalTime
     * @param currentTimedTask
     * @return
     */
    public static boolean isTaskToExecuteRightNow(long intervalTime, TimedTaskInfoEntity currentTimedTask) {
        return intervalTime < 0 && currentTimedTask.getStatus() == ResourceStatusEnums.AUDITED_NOT_ON_LINE.getStatus();
    }

    /**
     * 定时任务已执行
     *
     * @param intervalTime
     * @param currentTimedTask
     * @return
     */
    public static boolean isTaskHasFinished(long intervalTime, TimedTaskInfoEntity currentTimedTask) {
        return intervalTime < 0 && currentTimedTask.getStatus() == ResourceStatusEnums.ON_LINE.getStatus();
    }


    public static void valid(TimedTaskInfoEntity entity) {
        Assert.notNull(entity, "Job"+commonLanguageProperties.getNotNull());
        Assert.notNull(entity.getResourceId(), commonLanguageProperties.getResourceId()+commonLanguageProperties.getNotNull());
        Assert.hasText(entity.getResourceType(), commonLanguageProperties.getResourceType()+commonLanguageProperties.getNotNull());
        Assert.notNull(entity.getResourceVersion(), commonLanguageProperties.getResourceVersion()+commonLanguageProperties.getNotNull());
        Assert.hasText(entity.getTimedType(), "定时类型"+commonLanguageProperties.getNotNull());
        if (TimedTaskTypeConstants.isRecommendPoolUpdate(entity.getTimedType())) {
            Assert.hasText(entity.getCron(), "cron表达式"+commonLanguageProperties.getNotNull());
            Assert.isTrue(isCronOk(entity.getCron()), "cron表达式不正确！");
        }
    }
    @Autowired
    public void setEnterKeyLetterArt(CommonLanguageProperties commonLanguageProperties) {
        TimedTaskValidator.commonLanguageProperties = commonLanguageProperties;
    }


    private static boolean isCronOk(String cron) {
        try {
            return CronExpression.isValidExpression(cron);
        } catch (Exception e) {
            return false;
        }
    }
}
